package at.fhooe.engine.state;

import java.util.HashMap;

public class StateManager {

	private State m_currentGameState;
	private HashMap<String, State> m_states;
	
	public StateManager() {
		m_currentGameState = null;
		m_states = new HashMap<String, State>();
	}
	
	public void addState(State state) {
		m_states.put(state.getName(), state);
	}
	
	public State getState(String stateName) {
		return m_states.get(stateName);
	}
	
	public void switchToState(String stateName) {
		if (!m_states.containsKey(stateName)) {
			return;
		}
		
		if (m_currentGameState != null) {
			m_currentGameState.disengage();
		}
		
		m_currentGameState = m_states.get(stateName);
		m_currentGameState.engage();
	}
	
	public void switchToState(String stateName, StateDataObject data) {
		if (!m_states.containsKey(stateName)) {
			return;
		}
		
		if (m_currentGameState != null) {
			m_currentGameState.disengage();
		}
		
		m_currentGameState = m_states.get(stateName);
		m_currentGameState.engage(data);
	}
	
}
